package org.wenhua.yuan.and.module_flutter.flutterM1.bridge

import android.os.Build
import android.widget.Toast
import androidx.annotation.RequiresApi
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import org.wenhua.yuan.and.util.ContextGlobal
import java.util.*

/**
 * @MethodCallHandler 处理fluter端发来的通知
 * @MethodChannel.Result 回调flutter端的回复
 */
class FlutterBridgeCompl : IFlutterBridge<Any?, MethodChannel.Result>, MethodChannel.MethodCallHandler {

    override fun onNativeBack(p: Any?) {
    }

    override fun goPointedNativePage(p: Any?) {
        // 接收来自flutter端的消息
        if (p is Map<*, *>) {
            val location = p["location"]

            Toast.makeText(ContextGlobal.get(), "goPointedNativePage=${location}", Toast.LENGTH_SHORT).show()
        }
    }

     /// example : 由native端向flutter端发送消息，flutter端可回复，之后native处理回复。
     /// fire("方法名", "just send a message from native.参数哦", object : MethodChannel.Result {
     ///   override fun success(result: Any?) {处理flutter端的回复信息}
     ///
     ///   override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) {..}
     ///
     ///   override fun notImplemented() {..}
     /// })
    @RequiresApi(Build.VERSION_CODES.N)
    override fun fire(method: String, arguments: Any, callback: MethodChannel.Result?) {
        methodChannels.forEach { it.invokeMethod(method, arguments, callback) }
    }
    override fun getNativeDeviceInfo(callback: MethodChannel.Result) {
    }

    override fun setBuriedPoint(p: Any?) {

    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        // 处理来之flutter端消息
        when (call.method) {
            "onNativeBack" -> {Toast.makeText(ContextGlobal.get(), "onNativeBack", Toast.LENGTH_SHORT).show()}
            "goPointedNativePage" -> {
                this.goPointedNativePage(call.arguments)
            }
            "getNativeDeviceInfo" -> {Toast.makeText(ContextGlobal.get(), "getNativeDeviceInfo", Toast.LENGTH_SHORT).show()}
            "setBuriedPoint" -> {Toast.makeText(ContextGlobal.get(), "setBuriedPoint", Toast.LENGTH_SHORT).show()}
            else -> result.notImplemented()
        }
    }




    companion object {
        private  var methodChannel:MethodChannel ? = null
        // 多dart入口创建，会初始化多个flutter引擎。同时也会创建多个对应的MethodChannel实例
        @JvmStatic
        private val methodChannels:MutableList<MethodChannel> = ArrayList()
        @JvmStatic
        var instance:FlutterBridgeCompl? = null
            private set

        /**
         * 该方法在JFlutterCacheManager.initFlutterEngine方法中调用
         * @FlutterBridge 参数须与Flutter端一致 （Creates a [MethodChannel] with the specified [name]）
         * */
        @JvmStatic
        fun init (flutterEngine:FlutterEngine) : FlutterBridgeCompl {
            methodChannel = MethodChannel(flutterEngine.dartExecutor, "FlutterBridge")
            if (instance == null) {
                FlutterBridgeCompl().also {
                    instance = it
                }
            }
            methodChannel!!.setMethodCallHandler(instance)
            methodChannels.add(methodChannel!!)

            return instance!!
        }

        @JvmStatic
        fun getMethodChannels () : List<MethodChannel> {
            return methodChannels
        }
    }




}